package it.blog.server;

import java.sql.SQLException;

import javax.jws.*;
import javax.xml.ws.soap.Addressing;
import com.sun.xml.ws.developer.*;

@WebService @Stateful @Addressing
public class AuthBlogPortal {
	public static StatefulWebServiceManager<AuthBlogPortal> manager;
	private IDataAccess dataAccess;
	private User user;
	
	public AuthBlogPortal(User user){
		this.user=user;
	}
	
	public AuthBlogPortal(String username,String password){
		this.user=login(username, password);
	}

	private void  getDataAccess(){
		dataAccess= DataAccess.getInstance();
	}

	public  User getSessionUser() {
		return this.user;
	}

	
	public  User login(String username,String password) {

		User user = null;
		getDataAccess();
		try {
			user = dataAccess.login(username, password);

		} catch (SQLException e) {
			return null;
		}
		return user;
	}


	public boolean createBlog(Blog aBlog){

		getDataAccess();	
		try {
			return dataAccess.createBlog(aBlog);
		} catch (SQLException e) {
			return false;
		}

	}

	public boolean addPost(Post aPost) {

		getDataAccess();	
		try {
			return dataAccess.addPost(aPost);
		} catch (SQLException e) {
			return false;
		}


	}

	public boolean addComment(Comment aComment){

		getDataAccess();	
		try {
			return dataAccess.addComment(aComment);
		} catch (SQLException e) {
			return false;
		}

	}

	public boolean updateUser(User user) {

		getDataAccess();	
		try {
			boolean res=dataAccess.updateUser(user);
			if (res)
				this.user=user;
			return res;
		} catch (SQLException e) {
			return false;
		}
	}

	public boolean updateBlog(Blog blog){		

		getDataAccess();	
		try {
			return dataAccess.updateBlog(blog);
		} catch (SQLException e) {
			return false;
		}
	}

	public boolean updatePost(Post post){

		getDataAccess();	
		try {
			return dataAccess.updatePost(post);
		} catch (SQLException e) {
			return false;
		}


	}

	public boolean removeUser(Integer userID){

		getDataAccess();	
		try {
			return dataAccess.removeUser(userID);
		} catch (SQLException e) {
			return false;
		}

	}

	public boolean removeBlog(Integer blogID){

		getDataAccess();	
		try {
			return dataAccess.removeBlog(blogID);
		} catch (SQLException e) {
			return false;
		}

	}

	public boolean removePost(Integer postID){

		getDataAccess();	
		try {
			return dataAccess.removePost(postID);
		} catch (SQLException e) {
			return false;
		}

	}

	public boolean removeComment(Integer commentID){

		getDataAccess();	
		try {
			return dataAccess.removeComment(commentID);
		} catch (SQLException e) {
			return false;
		}

	}
	
	
	public boolean createUser(User aUser){

		getDataAccess();	
		try {
			return dataAccess.createUser(aUser);
		} catch (SQLException e) {
			return false;
		}

	}
	
	
	public boolean addBlogCategory(BlogCategory aCategory) {

		getDataAccess();	
		try {
			return dataAccess.addBlogCategory(aCategory);
		} catch (SQLException e) {
			return false;
		}

	}

	
	
	
	public boolean addPostCategory(PostCategory aCategory) {

		getDataAccess();	
		try {
			return dataAccess.addPostCategory(aCategory);
		} catch (SQLException e) {
			return false;
		}


	}


	public boolean updateComment(Comment comment){

		getDataAccess();	
		try {
			return dataAccess.updateComment(comment);
		} catch (SQLException e) {
			return false;
		}


	}
	

	public boolean updateBlogCategory(BlogCategory aCategory){

		getDataAccess();	
		try {
			return dataAccess.updateBlogCategory(aCategory);
		} catch (SQLException e) {
			return false;
		}

	}


	public boolean updatePostCategory(PostCategory aCategory){

		getDataAccess();	
		try {
			return dataAccess.updatePostCategory(aCategory);
		} catch (SQLException e) {
			return false;
		}

	}

	
	public boolean removeBlogCategory(Integer categoryID) {

		getDataAccess();	
		try {
			return dataAccess.removeBlogCategory(categoryID);
		} catch (SQLException e) {
			return false;
		}

	}
	
	
	public boolean removePostCategory(Integer categoryID) {


		getDataAccess();	
		try {
			return dataAccess.removePostCategory(categoryID);
		} catch (SQLException e) {
			return false;
		}

	}

	
}

